#ifndef _DBF_FILE_H_
#define _DBF_FILE_H_

#include "dbf.h"
#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

class dbf_reader
{
	public:
		dbf_reader(const char *buf, unsigned int size);
		~dbf_reader();

	public:
		typedef pair<unsigned int, unsigned char> offset_length;
		typedef vector<offset_length> ol_vector_t;

	public:
		void get_field_value(int record_index, int field_index, char *value);
		void get_field_value(int record_index, int field_index, long& value);
		void get_field_value(int record_index, int field_index, unsigned long& value);
		void get_field_value(int record_index, int field_index, int& value);
		void get_field_value(int record_index, int field_index, float& value);
		void get_field_value(int record_index, int field_index, double& value);

		char get_version();
		int get_update();
		int get_head_size();

		unsigned int get_record_size();
		unsigned int get_record_count();
		unsigned int get_field_count();

		int get_field_index_by_name(const char *name);

	private:
		void get_field_value(int record_index, int field_index);
		unsigned int reader(const char *buf, unsigned int size);
		void reset_fields();

	private:
		DBF_HEAD m_head;
		vector<DBF_FIELD> m_field_descriptors;
		typedef map<string, int> field_index_map;
		ol_vector_t field_ol_vector;
		field_index_map field_index;

		unsigned int m_record_size;
		unsigned int m_record_count;
		const char *m_records;
		char m_result[265];
};

#endif

